{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ba2d6d4a",
   "metadata": {},
   "source": [
    "    Calculate geomagnetic field at a given location and obstime. 'IGRF13' geomagnetic model is used as default. \n",
    "\n",
    "    Using the 'geomagnetic' module, the magnetic field can be defined in two different ways (see below) depending on the input location. The default geomagnetic model ('IGRF13') and default obstime ('2020-01-01') are used if not given.\n",
    "    \n",
    "        Geomagnet(model: str= 'IGRF13',\n",
    "                  latitude : Union[Number, np.ndarray] = None,\n",
    "                  longitude: Union[Number, np.ndarray] = None,\n",
    "                  height   : Union[Number, np.ndarray] = None,\n",
    "                  location : Union[ECEF, Geodetic, GRANDCS]=None,\n",
    "                  obstime  : Union[str, datetime.date] = '2020-01-01') -> CartesianRepresentation:\n",
    "\n",
    "        \n",
    "        1. Location can be given as ECEF, Geodetic, LTP, or GRAND coordinate system. 'x' below can be in ECEF, Geodetic, LTP, or GRANDCS.\n",
    "        \n",
    "            gm = Geomagnet(location=x)    \n",
    "            gm = Geomagnet(location=x, model='IGRF13', obstime='2021-08-30')\n",
    "            \n",
    "        2. Location can be given as latitude, longitude, and height.\n",
    "        \n",
    "            gm = Geomagnet(latitude=0, longitude=0, height=0)\n",
    "            gm = Geomagnet(latitude=0, longitude=0, height=0, model='IGRF13', obstime='2020-08-30')\n",
    "            \n",
    "    TypeError will occur if location is not provided. For observation time (obstime), provide time in isoformat ('2020-08-30') or in datetime.date(2020, 8, 30). If obstime is not provided, a default date ('2020-01-01') is used. After the geomagnetic field is instantiated, you can get the required quantity by:\n",
    "    \n",
    "        field       = gm.field          Output: CartesianRepresentation(x=Bx, y=By, z=Bz)\n",
    "        declination = gm.declination    Output: in degree\n",
    "        inclination = gm.inclination    Output: in degree\n",
    "        model       = gm.model          Output: in string\n",
    "        obstime     = gm.obstime        Output: in datetime.date()\n",
    "        location    = gm.location       Output: in Geodetic(latitude=x, longitude=x, height=x)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "62975908",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to JupyROOT 6.26/02\n"
     ]
    }
   ],
   "source": [
    "from grand import Geomagnet, Geodetic\n",
    "\n",
    "import numpy as np\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "76bd7271",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------Geomagnetic Field--------\n",
      "\n",
      "-- Magneitc Field (B)\n",
      "[[-2.24188795e-06]\n",
      " [ 2.75399734e-05]\n",
      " [ 1.60124534e-05]] <class 'grand.geo.coordinates.CartesianRepresentation'>\n",
      "-- Observation time\n",
      "2020-01-01 <class 'datetime.date'>\n",
      "-- Location\n",
      "[[0.]\n",
      " [0.]\n",
      " [1.]] <class 'grand.geo.coordinates.Geodetic'>\n",
      "--\n",
      "Declination [deg]: -4.654 ,  Inclination [deg]: -30.093\n",
      "....................\n",
      "\n",
      "-- Magneitc Field (B)\n",
      "[[-2.24188795e-06]\n",
      " [ 2.75399734e-05]\n",
      " [ 1.60124534e-05]] <class 'grand.geo.coordinates.CartesianRepresentation'>\n",
      "-- Observation time\n",
      "2020-01-01 <class 'datetime.date'>\n",
      "-- Location\n",
      "[[0.]\n",
      " [0.]\n",
      " [1.]] <class 'grand.geo.coordinates.Geodetic'>\n",
      "--\n",
      "Declination [deg]: -4.654 ,  Inclination [deg]: -30.093\n",
      "....................\n",
      "\n",
      "-- Magneitc Field (B) at GRANDCS Origin\n",
      "[[ 2.15525466e-07]\n",
      " [ 2.81105530e-05]\n",
      " [-4.77043114e-05]] <class 'grand.geo.coordinates.CartesianRepresentation'>\n",
      "-- Observation time\n",
      "2021-08-30 <class 'str'>\n",
      "-- Location\n",
      "[[  38.87253]\n",
      " [  92.35731]\n",
      " [2797.026  ]] <class 'grand.geo.coordinates.Geodetic'>\n",
      "-- \n",
      "Declination [deg]: 0.43928 ,  Inclination [deg]: 59.48984\n",
      "....................\n",
      "\n",
      "-- Magneitc Field (B) at GRANDCS Origin. magnetic model: WMM2020\n",
      "[[ 2.10795623e-07]\n",
      " [ 2.81145052e-05]\n",
      " [-4.77162661e-05]] <class 'grand.geo.coordinates.CartesianRepresentation'>\n",
      "-- Observation time\n",
      "2021-08-30 <class 'str'>\n",
      "-- Location\n",
      "[[  38.87253]\n",
      " [  92.35731]\n",
      " [2797.026  ]] <class 'grand.geo.coordinates.Geodetic'>\n",
      "-- \n",
      "Declination [deg]: 0.42958 ,  Inclination [deg]: 59.49263\n",
      "....................\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"---------Geomagnetic Field--------\")\n",
    "\n",
    "# Define a location. More on this later. Location of equator and GRANDCS origin (approx.) is used.\n",
    "loc1 = Geodetic(latitude=0, longitude=0, height=1)\n",
    "loc2 = Geodetic(latitude=38.87253, longitude=92.35731, height=2797.026)  # possible GRANDCS origin.\n",
    "\n",
    "# instantiate a global geomagnetic field based on a model, location and observation time.\n",
    "# Model and observation time has default values ('IGRF13', datetime.date(2020, 1, 1)).\n",
    "# Location must be provided. It can be given in ECEF, Geodetic, or GRAND coordinate system.\n",
    "# Or location can be given as latitude (deg), longitude (deg), and height (m) asl.\n",
    "# Observation time should be given in isoformat or in datetime.date().\n",
    "\n",
    "geoB1 = Geomagnet(location=loc1)  # default model and obstime is used.\n",
    "\n",
    "geoB2 = Geomagnet(latitude=0, longitude=0, height=1)  # default model and obstime is used.\n",
    "\n",
    "geoB3 = Geomagnet(model=\"IGRF13\", location=loc2, obstime=\"2021-08-30\")\n",
    "# or geoB3 = Geomagnet(location=loc2, obstime=datetime.date(2021, 8, 30))\n",
    "\n",
    "geoB4 = Geomagnet(model=\"WMM2020\", location=loc2, obstime=\"2021-08-30\")\n",
    "\n",
    "print(\"\")\n",
    "print(\"-- Magneitc Field (B)\")\n",
    "print(geoB1.field, type(geoB1.field))\n",
    "print(\"-- Observation time\")\n",
    "print(geoB1.obstime, type(geoB1.obstime))\n",
    "print(\"-- Location\")\n",
    "print(geoB1.location, type(geoB1.location))\n",
    "print(\"--\")\n",
    "print(\n",
    "    \"Declination [deg]:\",\n",
    "    round(geoB1.declination, 3),\n",
    "    \",  Inclination [deg]:\",\n",
    "    round(geoB1.inclination, 3),\n",
    ")\n",
    "print(\"....................\\n\")\n",
    "\n",
    "print(\"-- Magneitc Field (B)\")\n",
    "print(geoB2.field, type(geoB2.field))\n",
    "print(\"-- Observation time\")\n",
    "print(geoB2.obstime, type(geoB2.obstime))\n",
    "print(\"-- Location\")\n",
    "print(geoB2.location, type(geoB2.location))\n",
    "print(\"--\")\n",
    "print(\n",
    "    \"Declination [deg]:\",\n",
    "    round(geoB2.declination, 3),\n",
    "    \",  Inclination [deg]:\",\n",
    "    round(geoB2.inclination, 3),\n",
    ")\n",
    "print(\"....................\\n\")\n",
    "\n",
    "print(\"-- Magneitc Field (B) at GRANDCS Origin\")\n",
    "print(geoB3.field, type(geoB3.field))\n",
    "print(\"-- Observation time\")\n",
    "print(geoB3.obstime, type(geoB3.obstime))\n",
    "print(\"-- Location\")\n",
    "print(geoB3.location, type(geoB3.location))\n",
    "print(\"-- \")\n",
    "print(\n",
    "    \"Declination [deg]:\",\n",
    "    round(geoB3.declination, 5),\n",
    "    \",  Inclination [deg]:\",\n",
    "    round(geoB3.inclination, 5),\n",
    ")\n",
    "print(\"....................\\n\")\n",
    "\n",
    "print(\"-- Magneitc Field (B) at GRANDCS Origin. magnetic model: WMM2020\")\n",
    "print(geoB4.field, type(geoB4.field))\n",
    "print(\"-- Observation time\")\n",
    "print(geoB4.obstime, type(geoB4.obstime))\n",
    "print(\"-- Location\")\n",
    "print(geoB4.location, type(geoB4.location))\n",
    "print(\"-- \")\n",
    "print(\n",
    "    \"Declination [deg]:\",\n",
    "    round(geoB4.declination, 5),\n",
    "    \",  Inclination [deg]:\",\n",
    "    round(geoB4.inclination, 5),\n",
    ")\n",
    "print(\"....................\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1a841ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
